package com.mjf.ldpro.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.mjf.cjt.domain.VoucherIncome;
import com.mjf.cjt.service.VoucherIncomeService;
import com.mjf.ldpro.domain.LdProjectInfo;
import com.mjf.ldpro.mapper.LdProjectInfoMapper;
import com.mjf.ldpro.service.LdProjectInfoService;
import com.mjf.ldpro.vo.ExpenseParam;
import com.mjf.ldpro.vo.LdProjectInertVO;
import com.mjf.ue.service.IU8BarcodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/4/21 23:19
 */
@Service
public class LdProjectInfoServiceImpl extends ServiceImpl<LdProjectInfoMapper, LdProjectInfo> implements LdProjectInfoService {

    @Autowired
    private LdProjectInfoMapper ldProjectInfoMapper;
    @Autowired
    private IU8BarcodeService u8BarcodeService;
    @Autowired
    private VoucherIncomeService voucherIncomeService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(LdProjectInertVO vo) {

        LdProjectInfo info = new LdProjectInfo();
        BeanUtil.copyProperties(vo, info);
        if (ObjectUtil.isNull(info.getId())) {
            info.setCreateTime(LocalDateTime.now());
        }
        saveOrUpdate(info);
    }

    @Override
    public IPage<LdProjectInfo> projectList(Integer page, Integer size) {
        Page<LdProjectInfo> pg = new Page<>(page, size);
        QueryWrapper<LdProjectInfo> queryWrapper = new QueryWrapper<>();
        List<LdProjectInfo> list = list();
        pg.setRecords(list);
        pg.setTotal(list.size());
        return pg;
    }

    public static void main(String[] args) {

    }

    @Override
    public Object testM() {
        return u8BarcodeService.list();
    }

    @Override
    public Object openList() {




        return null;
    }

    @Override
    public IPage<VoucherIncome> expenseList(ExpenseParam param) {

        IPage<VoucherIncome> page = new Page<>(param.getPage(), param.getSize());
        LambdaQueryWrapper<VoucherIncome> qw = Wrappers.<VoucherIncome>lambdaQuery()
            .ge(StrUtil.isNotBlank(param.getStart()), VoucherIncome::getCreateTime, param.getStart())
            .le(StrUtil.isNotBlank(param.getEnd()), VoucherIncome::getCreateTime, param.getEnd());
        long count = voucherIncomeService.count(qw);
        page.setTotal(count);
        qw.orderByDesc(VoucherIncome::getCreateTime);
        qw.last("limit " + (param.getPage() - 1) * param.getSize() + "," + param.getSize());
        List<VoucherIncome> list = voucherIncomeService.list(qw);
        page.setRecords(list);
        return page;
    }
}
